<?php
/**
 * Easthv Zend Framework Project
 *
 * LICENSE:
 *
 * @category   Zend
 * @package    Zend_Easthv
 * @copyright  Copyright (c) 2009-2010 Easthv Team @ East Hui Van Business Consulting Co., Ltd.(http://www.easthv.com)
 * @license    http://framework.zend.com/license/   BSD License
 * @version    1.0
 * @link       http://www.easthv.com
 * @since      1.0
*/

/**
 * Array class
 *
 * @category   Zend
 * @package    Zend_Easthv
 * @copyright  Copyright (c) 2009-2010 Easthv Team @ East Hui Van Business Consulting Co., Ltd.(http://www.easthv.com)
 * @license    http://framework.zend.com/license/   BSD License
 * @version    1.0
 * @link       http://www.easthv.com
 * @since      1.0
 * @deprecated
 */
class Zend_Easthv_Array
{


    /**
     * Sort an two-dimension array by some level two items use array_multisort() function.
     *
     * sysSortArray($Array,"Key1","SORT_ASC","SORT_RETULAR","Key2")
     * @author Chunsheng Wang <wwccss@263.net>
     * @param array $arrayData the array to sort.
     * @param string $keyName the first item to sort by.
     * @param string $sortOrder the order to sort by("SORT_ASC"|"SORT_DESC")
     * @param string $sortType the sort type("SORT_REGULAR"|"SORT_NUMERIC"|"SORT_STRING")
     * @return array sorted array.
     */ 
    public static function multiSortArray($arrayData, $keyName, $sortOrder = "SORT_ASC", $sortType = "SORT_REGULAR")
    {
        if (!is_array($arrayData)) {
            return $arrayData;
        }

        // Get args number.
        $argCount = func_num_args();

        // Get keys to sort by and put them to SortRule array.
        for($i = 1;$i < $argCount;$i ++)
        {
            $arg = func_get_arg($i);
            if(!eregi("SORT",$arg)){
                $keyNameList[] = $arg;
                $sortRule[] = '$'.$arg;
            }else{
                $sortRule[] = $arg;
            }
        }

        // Get the values according to the keys and put them to array.
        foreach($arrayData as $key => $info){
            foreach($keyNameList AS $keyName)
            {
                ${$keyName}[$key] = $info[$keyName];
            }
        }

        // Create the eval string and eval it.
        $evalString = 'array_multisort(' . join(",", $sortRule) . ',$arrayData);';
        eval ($evalString);
        return $arrayData;
	}
    /**
	 * check an array whether empty
	 *
	 * @param array $arr  array to be check
	 *
	 * @access public
	 *
     * @return boolean
     */
	public static function isEmpty($arr)
	{
		if(!$arr)
			return true;

		foreach($arr as $_arr) {
			if(is_array($_arr)) {

				if(self::isEmpty($_arr))

					return true;
			}
			else {

				if($_arr)

					return false;

			}

		}
		return true;
	}

	public static function isFull($arr)
	{
		if(!$arr)
			return false;

		foreach($arr as $_arr) {

			if(is_array($_arr)) {

				return self::isFull($_arr);

			}
			else {

				if(!$_arr)

					return false;

			}

		}
		return true;
	}

    /**
	 * check array whether content repeat elements
	 *
	 * @param string $field  array key name to be check
	 * @param array  $arr    array to be check
	 *
	 * @access public
	 *
     * @return boolean
     */
	public static function checkRepeat($field,$arr)
	{
		$res = array();
		foreach($arr as $_arr) {

			if($_arr[$field]) {

				$res[] = $_arr[$field];

			}


		}

		if(!$res) {

			return false;

		}

		$unique = array_unique($res);

		return $unique === $res ? false : true;
	}

    /**
	 * get different part of two arrays
	 *
	 * @param array $input   inpur array
	 * @param array $data    array refer to
	 *
	 * @access public
	 *
	 * @return array         different part of two arrays
	 */
	public static function getDiff($input,$data)
	{
		$res = array();
		foreach($input as $field => $value) {

			if($data[$field] != $value) {

				$res[$field] = $value;

			}


		}

		return $res;
	}

    /**
	 * Search array by key and value
	 *
	 * @param array $key     search key
	 * @param array $value   search value
	 * @param array $data    array refer to
	 *
	 * @access public
	 *
	 * @return array         
	 */
	public static function searchArr($key,$value,$data)
	{
		foreach($data as $arr) {

			foreach($arr as $datakey => $datavalue) {

				if($key == $datakey && $value == $datavalue) {

					return $arr;
				
				}

			}
		
		}

		return false;
	}

    /**
	 * Search array values
	 *
	 * @param array $values   search value
	 * @param array $data    array refer to
	 *
	 * @access public
	 *
	 * @return array         
	 */
	public static function multiSearchArr($type,$values,$data)
	{
		$valuesCount = count($values);

		$res = array();

		foreach($data as $arr) {
			$i = 0;
			foreach($values as $datakey => $datavalue) {
				if($arr[$datakey] == $datavalue) {
					$i++;
				}
			}
			if ($valuesCount == $i && $i != 0) {
				if ($type == 'one') {
					return $arr;
				} else {
					$res[] = $arr;
				}
			}	
		}
		
		if ($res) {
			return $res;
		} else {
			return false;
		}	
	}

    /**
	 * get part of refer array which not null by input
	 *
	 * @param array $params   inpur array
	 * @param array $fields   array refer to
	 *
	 * @access public
	 *
	 * @return array         not null part of arrays
	 */
	public static function transNotNullToFields($params,$fields)
	{
		$res = array();

		foreach($fields as $dbfield => $paramName) {
			if($params[$paramName] !== '') {
				$res[$dbfield] = $params[$paramName];
			}
		}
		return $res;
	}

    /**
	 * get all of refer array which not null by input
	 *
	 * @param array $params   inpur array
	 * @param array $fields   array refer to
	 *
	 * @access public
	 *
	 * @return array
	 */
	public static function transAllToFields($params,$fields)
	{
		$res = array();

		foreach($fields as $dbfield => $paramName) {

			$res[$dbfield] = $params[$paramName];

		}
		return $res;
	}

    /**
	 * get one element of array by input
	 *
	 * @param array $param   input array
	 * @param array $fields   array refer to
	 *
	 * @access public
	 *
	 * @return string
	 */
	public static function transOneToField($param,$filelds)
	{
		$filelds = array_flip($filelds);
		return isset($filelds[$param]) ? $filelds[$param] : '';
	}



	public static function inArray($str,$arr)
	{
		if(is_array($arr[0])) {

			foreach($arr as $_arr){

				$res = self::inArray($str,$_arr);

				if($res) {

					return true;
				
				}
			
			
			}

			return false;
		
		}

		return in_array($str,$arr);
	
	}

	/**
	 * @package BugFree
	 * @version $Id: FunctionsMain.inc.php,v 1.32 2005/09/24 11:38:37 wwccss Exp $
	 *
	 *
	 * Sort an two-dimension array by some level two items use array_multisort() function.
	 *
	 * sysSortArray($Array,"Key1","SORT_ASC","SORT_RETULAR","Key2"……)
	 * @author Chunsheng Wang <wwccss@263.net>
	 * @param array $ArrayData the array to sort.
	 * @param string $KeyName1 the first item to sort by.
	 * @param string $SortOrder1 the order to sort by("SORT_ASC"|"SORT_DESC")
	 * @param string $SortType1 the sort type("SORT_REGULAR"|"SORT_NUMERIC"|"SORT_STRING")
	 * @return array sorted array.
	 */ 
	public static function sysSortArray($ArrayData,$KeyName1,$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR")
	{
		if(!is_array($ArrayData))
		{
			return $ArrayData;
		}

		// Get args number.
		$ArgCount = func_num_args();

		// Get keys to sort by and put them to SortRule array.
		for($I = 1;$I < $ArgCount;$I ++)
		{
			$Arg = func_get_arg($I);
			if(!@eregi("SORT",$Arg))
			{
				$KeyNameList[] = $Arg;
				$SortRule[] = '$'.$Arg;
			}
			else
			{
				$SortRule[] = $Arg;
			}
		}

		// Get the values according to the keys and put them to array.
		foreach($ArrayData AS $Key => $Info)
		{
			foreach($KeyNameList AS $KeyName)
			{
				${$KeyName}[$Key] = $Info[$KeyName];
			}
		}

		// Create the eval string and eval it.
		$EvalString = 'array_multisort('.join(",",$SortRule).',$ArrayData);';
		eval ($EvalString);
		return $ArrayData;
	}

	
	public static function natSortArray($ArrayData,$KeyName)
	{
		if(!is_array($ArrayData))
		{
			return $ArrayData;
		}
		
		$stringArray = array();
		
		foreach($ArrayData as $key => $array){
		
			$stringArray[] = $array[$KeyName];
		
		}
	
		natsort($stringArray);

		$sortedArray = array();
		
		foreach($stringArray as $key => $sortString){
		
			$sortedArray[$key] = $ArrayData[$key];
		
		}
		
		return $sortedArray;
	}
	
	public static function strSortArray($ArrayData,$KeyName)
	{
		if(!is_array($ArrayData))
		{
			return $ArrayData;
		}
		
		$stringArray = array();
		
		foreach($ArrayData as $key => $array){
		
			$stringArray[] = $array[$KeyName];
		
		}
		asort($stringArray,SORT_STRING);

		$sortedArray = array();
		
		foreach($stringArray as $key => $sortString){
		
			$sortedArray[] = $ArrayData[$key];
		
		}
		
		return $sortedArray;
	}	

	public static function simpleXmlToArray($xml) {
		$ar = array();
		foreach($xml->children() as $k => $v) {
			// recurse the child
			$child = self::simpleXmlToArray($v);
			// if it's not an array, then it was empty, thus a value/string
			if( count($child) == 0 ) {
				$child = (string)$v;
			}

			// add the childs attributes as if they where children
			foreach( $v->attributes() as $ak => $av ) {
				// if the child is not an array, transform it into one
				if( !is_array( $child ) ) {
					$child = array( "value" => $child );
				}
				$child[$ak] = (string)$av;
			}

			// if the $k is already in our children list, we need to transform
			// it into an array, else we add it as a value
			if (!in_array($k,array_keys($ar))) {
				$ar[$k] = $child;
			} elseif (@in_array(0,@array_keys($ar[$k]))) {
				$ar[$k][] = $child;
			} else {
				$ar[$k] = array($ar[$k]);
				$ar[$k][] = $child;
			}

		}
		return $ar;
	}

	public static function pickUp($arr,$keyName,$valueName,$withNull = true)
	{

		$res = array();

		if($withNull) {
			$res[] = '';
		}

		foreach($arr as $_arr) {

			$res[$_arr[$keyName]] = $_arr[$valueName];

		}

		return $res;
	
	}	
}
